package com.outsourcing.bottle.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.outsourcing.bottle.domain.BottleTypeEntry;

/**
 * 瓶子类型表
 * @author 06peng
 *
 */
public class BottleTypeTable {

	private final String TABLE_BOTTLE_TYPE = "bottle_type";
	
	private DBManagerImpl db = null;
	public BottleTypeTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_TYPE)) {
			createBottleType();
		}
	}
	public BottleTypeTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_TYPE)) {
			createBottleType();
		}
	}
	
	private void createBottleType() {
		String createSql = "create table if not exists "+TABLE_BOTTLE_TYPE+" (id integer primary key autoincrement,bttypeid integer, bttype_name_cn varchar, bttype_name_en varchar," +
				"bttype_desc_cn varchar, bttype_desc_en varchar, bttype_tips_cn varchar, bttype_tips_en varchar, bttype_sjdicon varchar," +
				"bttype_sjwicon varchar, bttype_samecity integer, bttype_samecity_show integer, bttype_samearea integer, bttype_samearea_show integer," +
				"bttype_needsex integer, bttype_needsex_show integer, bttype_needage integer, bttype_needage_show integer, bttype_allow_sendfriend integer, bttype_allow_pick integer," +
				"bttype_pick_show integer, bttype_allow_spread integer, bttype_spread_show integer, bttype_message_show integer, bttype_message_must integer, bttype_pic_show integer," +
				"bttype_pic_must integer, bttype_doing_show integer, bttype_doing_must integer, bttype_poll_show integer, bttype_poll_must integer, bttype_otheruserfeed integer, bttype_otheruserfeed_show integer," +
				"bttype_otheruserreply integer, bttype_otheruserreply_show integer, bttype_isshow integer, bttype_location_select integer)";
		db.creatTable(db.getConnection(), createSql, TABLE_BOTTLE_TYPE);
	}
	
	public ArrayList<BottleTypeEntry> getBottleTypes() {
		Cursor cursor = null;
		ArrayList<BottleTypeEntry> bottleTypes = null;
		try {
			cursor = db.find(db.getConnection(), "select * from " + TABLE_BOTTLE_TYPE + " where bttype_isshow = 1", null);
			if (cursor != null) {
				bottleTypes = new ArrayList<BottleTypeEntry>();
				while (cursor.moveToNext()) {
					BottleTypeEntry bottleType = new BottleTypeEntry();
					bottleType.setBttypeid(cursor.getInt(cursor.getColumnIndex("bttypeid")));
					bottleType.setBttype_name_cn(cursor.getString(cursor.getColumnIndex("bttype_name_cn")));
					bottleType.setBttype_name_en(cursor.getString(cursor.getColumnIndex("bttype_name_en")));
					bottleType.setBttype_desc_cn(cursor.getString(cursor.getColumnIndex("bttype_desc_cn")));
					bottleType.setBttype_desc_en(cursor.getString(cursor.getColumnIndex("bttype_desc_en")));
					bottleType.setBttype_tips_cn(cursor.getString(cursor.getColumnIndex("bttype_tips_cn")));
					bottleType.setBttype_tips_en(cursor.getString(cursor.getColumnIndex("bttype_tips_en")));
					bottleType.setBttype_sjdicon(cursor.getString(cursor.getColumnIndex("bttype_sjdicon")));
					bottleType.setBttype_sjwicon(cursor.getString(cursor.getColumnIndex("bttype_sjwicon")));
					bottleType.setBttype_samecity(cursor.getInt(cursor.getColumnIndex("bttype_samecity")));
					bottleType.setBttype_samecity_show(cursor.getInt(cursor.getColumnIndex("bttype_samecity_show")));
					bottleType.setBttype_samearea(cursor.getInt(cursor.getColumnIndex("bttype_samearea")));
					bottleType.setBttype_samearea_show(cursor.getInt(cursor.getColumnIndex("bttype_samearea_show")));
					bottleType.setBttype_needsex(cursor.getInt(cursor.getColumnIndex("bttype_needsex")));
					bottleType.setBttype_needsex_show(cursor.getInt(cursor.getColumnIndex("bttype_needsex_show")));
					bottleType.setBttype_needage(cursor.getInt(cursor.getColumnIndex("bttype_needage")));
					bottleType.setBttype_needage_show(cursor.getInt(cursor.getColumnIndex("bttype_needage_show")));
					bottleType.setBttype_allow_sendfriend(cursor.getInt(cursor.getColumnIndex("bttype_allow_sendfriend")));
					bottleType.setBttype_allow_pick(cursor.getInt(cursor.getColumnIndex("bttype_allow_pick")));
					bottleType.setBttype_pick_show(cursor.getInt(cursor.getColumnIndex("bttype_pick_show")));
					bottleType.setBttype_allow_spread(cursor.getInt(cursor.getColumnIndex("bttype_allow_spread")));
					bottleType.setBttype_spread_show(cursor.getInt(cursor.getColumnIndex("bttype_spread_show")));
					bottleType.setBttype_message_show(cursor.getInt(cursor.getColumnIndex("bttype_message_show")));
					bottleType.setBttype_message_must(cursor.getInt(cursor.getColumnIndex("bttype_message_must")));
					bottleType.setBttype_pic_show(cursor.getInt(cursor.getColumnIndex("bttype_pic_show")));
					bottleType.setBttype_pic_must(cursor.getInt(cursor.getColumnIndex("bttype_pic_must")));
					bottleType.setBttype_doing_show(cursor.getInt(cursor.getColumnIndex("bttype_doing_show")));
					bottleType.setBttype_doing_must(cursor.getInt(cursor.getColumnIndex("bttype_doing_must")));
					bottleType.setBttype_poll_show(cursor.getInt(cursor.getColumnIndex("bttype_poll_show")));
					bottleType.setBttype_poll_must(cursor.getInt(cursor.getColumnIndex("bttype_poll_must")));
					bottleType.setBttype_otheruserfeed(cursor.getInt(cursor.getColumnIndex("bttype_otheruserfeed")));
					bottleType.setBttype_otheruserfeed_show(cursor.getInt(cursor.getColumnIndex("bttype_otheruserfeed_show")));
					bottleType.setBttype_otheruserreply(cursor.getInt(cursor.getColumnIndex("bttype_otheruserreply")));
					bottleType.setBttype_otheruserreply_show(cursor.getInt(cursor.getColumnIndex("bttype_otheruserreply_show")));
					bottleType.setBttype_isshow(cursor.getInt(cursor.getColumnIndex("bttype_isshow")));
					bottleType.setBttype_location_select(cursor.getInt(cursor.getColumnIndex("bttype_location_select")));
					bottleTypes.add(bottleType);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return bottleTypes;
	}
	
	public void saveBottleType(BottleTypeEntry bottleType) {
		ContentValues values = new ContentValues();
		values.put("bttypeid", bottleType.getBttypeid());
		values.put("bttype_name_cn", bottleType.getBttype_name_cn());
		values.put("bttype_name_en", bottleType.getBttype_name_en());
		values.put("bttype_desc_cn", bottleType.getBttype_desc_cn());
		values.put("bttype_desc_en", bottleType.getBttype_desc_en());
		values.put("bttype_tips_cn", bottleType.getBttype_tips_cn());
		values.put("bttype_tips_en", bottleType.getBttype_tips_en());
		values.put("bttype_sjdicon", bottleType.getBttype_sjdicon());
		values.put("bttype_sjwicon", bottleType.getBttype_sjwicon());
		values.put("bttype_samecity", bottleType.getBttype_samecity());
		values.put("bttype_samecity_show", bottleType.getBttype_samecity_show());
		values.put("bttype_samearea", bottleType.getBttype_samearea());
		values.put("bttype_samearea_show", bottleType.getBttype_samearea_show());
		values.put("bttype_needsex", bottleType.getBttype_needsex());
		values.put("bttype_needsex_show", bottleType.getBttype_needsex_show());
		values.put("bttype_needage", bottleType.getBttype_needage());
		values.put("bttype_needage_show", bottleType.getBttype_needage_show());
		values.put("bttype_allow_sendfriend", bottleType.getBttype_allow_sendfriend());
		values.put("bttype_allow_pick", bottleType.getBttype_allow_pick());
		values.put("bttype_pick_show", bottleType.getBttype_pick_show());
		values.put("bttype_allow_spread", bottleType.getBttype_allow_spread());
		values.put("bttype_spread_show", bottleType.getBttype_spread_show());
		values.put("bttype_message_show", bottleType.getBttype_message_show());
		values.put("bttype_message_must", bottleType.getBttype_message_must());
		values.put("bttype_pic_show", bottleType.getBttype_pic_show());
		values.put("bttype_pic_must", bottleType.getBttype_pic_must());
		values.put("bttype_doing_show", bottleType.getBttype_doing_show());
		values.put("bttype_doing_must", bottleType.getBttype_doing_must());
		values.put("bttype_poll_show", bottleType.getBttype_poll_show());
		values.put("bttype_poll_must", bottleType.getBttype_poll_must());
		values.put("bttype_otheruserfeed", bottleType.getBttype_otheruserfeed());
		values.put("bttype_otheruserfeed_show", bottleType.getBttype_otheruserfeed_show());
		values.put("bttype_otheruserreply", bottleType.getBttype_otheruserreply());
		values.put("bttype_otheruserreply_show", bottleType.getBttype_otheruserreply_show());
		values.put("bttype_isshow", bottleType.getBttype_isshow());
		values.put("bttype_location_select", bottleType.getBttype_location_select());
		db.save(db.getConnection(), TABLE_BOTTLE_TYPE, values);
	}
	
	public void saveBottleType(List<BottleTypeEntry> bottleTypes) {
		List<ContentValues> valueList = new ArrayList<ContentValues>();
		for(BottleTypeEntry bottleType : bottleTypes) {
			ContentValues values = new ContentValues();
			values.put("bttypeid", bottleType.getBttypeid());
			values.put("bttype_name_cn", bottleType.getBttype_name_cn());
			values.put("bttype_name_en", bottleType.getBttype_name_en());
			values.put("bttype_desc_cn", bottleType.getBttype_desc_cn());
			values.put("bttype_desc_en", bottleType.getBttype_desc_en());
			values.put("bttype_tips_cn", bottleType.getBttype_tips_cn());
			values.put("bttype_tips_en", bottleType.getBttype_tips_en());
			values.put("bttype_sjdicon", bottleType.getBttype_sjdicon());
			values.put("bttype_sjwicon", bottleType.getBttype_sjwicon());
			values.put("bttype_samecity", bottleType.getBttype_samecity());
			values.put("bttype_samecity_show", bottleType.getBttype_samecity_show());
			values.put("bttype_samearea", bottleType.getBttype_samearea());
			values.put("bttype_samearea_show", bottleType.getBttype_samearea_show());
			values.put("bttype_needsex", bottleType.getBttype_needsex());
			values.put("bttype_needsex_show", bottleType.getBttype_needsex_show());
			values.put("bttype_needage", bottleType.getBttype_needage());
			values.put("bttype_needage_show", bottleType.getBttype_needage_show());
			values.put("bttype_allow_sendfriend", bottleType.getBttype_allow_sendfriend());
			values.put("bttype_allow_pick", bottleType.getBttype_allow_pick());
			values.put("bttype_pick_show", bottleType.getBttype_pick_show());
			values.put("bttype_allow_spread", bottleType.getBttype_allow_spread());
			values.put("bttype_spread_show", bottleType.getBttype_spread_show());
			values.put("bttype_message_show", bottleType.getBttype_message_show());
			values.put("bttype_message_must", bottleType.getBttype_message_must());
			values.put("bttype_pic_show", bottleType.getBttype_pic_show());
			values.put("bttype_pic_must", bottleType.getBttype_pic_must());
			values.put("bttype_doing_show", bottleType.getBttype_doing_show());
			values.put("bttype_doing_must", bottleType.getBttype_doing_must());
			values.put("bttype_poll_show", bottleType.getBttype_poll_show());
			values.put("bttype_poll_must", bottleType.getBttype_poll_must());
			values.put("bttype_otheruserfeed", bottleType.getBttype_otheruserfeed());
			values.put("bttype_otheruserfeed_show", bottleType.getBttype_otheruserfeed_show());
			values.put("bttype_otheruserreply", bottleType.getBttype_otheruserreply());
			values.put("bttype_otheruserreply_show", bottleType.getBttype_otheruserreply_show());
			values.put("bttype_isshow", bottleType.getBttype_isshow());
			values.put("bttype_location_select", bottleType.getBttype_location_select());
			valueList.add(values);
		}
		db.saveAll(db.getConnection(), TABLE_BOTTLE_TYPE, valueList);
	}
	
	public BottleTypeEntry getBottleType(int bottleId) {
		Cursor cursor = null;
		BottleTypeEntry bottleType = null;
		try {
			cursor = db.find(db.getConnection(), "select * from "
					+ TABLE_BOTTLE_TYPE + " where bttypeid=?",
					new String[] { String.valueOf(bottleId) });
			if (cursor != null) {
				while (cursor.moveToNext()) {
					bottleType = new BottleTypeEntry();
					bottleType.setBttypeid(cursor.getInt(cursor.getColumnIndex("bttypeid")));
					bottleType.setBttype_name_cn(cursor.getString(cursor.getColumnIndex("bttype_name_cn")));
					bottleType.setBttype_name_en(cursor.getString(cursor.getColumnIndex("bttype_name_en")));
					bottleType.setBttype_desc_cn(cursor.getString(cursor.getColumnIndex("bttype_desc_cn")));
					bottleType.setBttype_desc_en(cursor.getString(cursor.getColumnIndex("bttype_desc_en")));
					bottleType.setBttype_tips_cn(cursor.getString(cursor.getColumnIndex("bttype_tips_cn")));
					bottleType.setBttype_tips_en(cursor.getString(cursor.getColumnIndex("bttype_tips_en")));
					bottleType.setBttype_sjdicon(cursor.getString(cursor.getColumnIndex("bttype_sjdicon")));
					bottleType.setBttype_sjwicon(cursor.getString(cursor.getColumnIndex("bttype_sjwicon")));
					bottleType.setBttype_samecity(cursor.getInt(cursor.getColumnIndex("bttype_samecity")));
					bottleType.setBttype_samecity_show(cursor.getInt(cursor.getColumnIndex("bttype_samecity_show")));
					bottleType.setBttype_samearea(cursor.getInt(cursor.getColumnIndex("bttype_samearea")));
					bottleType.setBttype_samearea_show(cursor.getInt(cursor.getColumnIndex("bttype_samearea_show")));
					bottleType.setBttype_needsex(cursor.getInt(cursor.getColumnIndex("bttype_needsex")));
					bottleType.setBttype_needsex_show(cursor.getInt(cursor.getColumnIndex("bttype_needsex_show")));
					bottleType.setBttype_needage(cursor.getInt(cursor.getColumnIndex("bttype_needage")));
					bottleType.setBttype_needage_show(cursor.getInt(cursor.getColumnIndex("bttype_needage_show")));
					bottleType.setBttype_allow_sendfriend(cursor.getInt(cursor.getColumnIndex("bttype_allow_sendfriend")));
					bottleType.setBttype_allow_pick(cursor.getInt(cursor.getColumnIndex("bttype_allow_pick")));
					bottleType.setBttype_pick_show(cursor.getInt(cursor.getColumnIndex("bttype_pick_show")));
					bottleType.setBttype_allow_spread(cursor.getInt(cursor.getColumnIndex("bttype_allow_spread")));
					bottleType.setBttype_spread_show(cursor.getInt(cursor.getColumnIndex("bttype_spread_show")));
					bottleType.setBttype_message_show(cursor.getInt(cursor.getColumnIndex("bttype_message_show")));
					bottleType.setBttype_message_must(cursor.getInt(cursor.getColumnIndex("bttype_message_must")));
					bottleType.setBttype_pic_show(cursor.getInt(cursor.getColumnIndex("bttype_pic_show")));
					bottleType.setBttype_pic_must(cursor.getInt(cursor.getColumnIndex("bttype_pic_must")));
					bottleType.setBttype_doing_show(cursor.getInt(cursor.getColumnIndex("bttype_doing_show")));
					bottleType.setBttype_doing_must(cursor.getInt(cursor.getColumnIndex("bttype_doing_must")));
					bottleType.setBttype_poll_show(cursor.getInt(cursor.getColumnIndex("bttype_poll_show")));
					bottleType.setBttype_poll_must(cursor.getInt(cursor.getColumnIndex("bttype_poll_must")));
					bottleType.setBttype_otheruserfeed(cursor.getInt(cursor.getColumnIndex("bttype_otheruserfeed")));
					bottleType.setBttype_otheruserfeed_show(cursor.getInt(cursor.getColumnIndex("bttype_otheruserfeed_show")));
					bottleType.setBttype_otheruserreply(cursor.getInt(cursor.getColumnIndex("bttype_otheruserreply")));
					bottleType.setBttype_otheruserreply_show(cursor.getInt(cursor.getColumnIndex("bttype_otheruserreply_show")));
					bottleType.setBttype_isshow(cursor.getInt(cursor.getColumnIndex("bttype_isshow")));
					bottleType.setBttype_location_select(cursor.getInt(cursor.getColumnIndex("bttype_location_select")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return bottleType;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_BOTTLE_TYPE, null, null);
	}
}
